package com.qf.controller;

import com.qf.constant.Constants;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.MD5Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/userLogin")
public class UserLoginServlet extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //校验验证码
        //接收前台用户验证码
        String loginCode = request.getParameter("loginCode");
        //获取服务器端存储验证码
        HttpSession session = request.getSession();
        String  CHECKCODE_SERVER = (String) session.getAttribute("CHECKCODE_SERVER");

        //一次性验证
        request.removeAttribute("CHECKCODE_SERVER");
        //判断
        if(loginCode==null || !loginCode.equalsIgnoreCase(CHECKCODE_SERVER)){
            //错误提示
            request.setAttribute("msg","验证码错误");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
            return ;
        }

        //如果验证码通过,获取用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //密码加密
        password = MD5Utils.md5(password) ;

        //调用业务接口
        UserService userService = new UserServiceImpl() ;
        User user = userService.getUser(username, password);

        //如果当前用户不存在
        if(user==null){
            //错误提示
            request.setAttribute("msg","用户名或者密码错误");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        }

        //如果用户存在
        //还要判断登录的用户状态是否为1,不为1,重新激活
        if(user.getState()!= Constants.USER_ACTIVE_STATE){
            //用户未激活
            request.setAttribute("msg","用户未激活,请先<a href='http://localhost:8080/RaclMall_System_admin_war_exploded/active?code="+user.getCode()+"'>激活用户</a>");
            request.getRequestDispatcher("/jsp/msg.jsp").forward(request,response);
        }

        //如果没有问题
        //将user对象存储到session中
        session.setAttribute("user",user);

        //获取cookie的名称为JSESSIONID的内容:id值
        //HttpSession依赖于Cookie
       /* String id = session.getId();
        System.out.println(id);*/

       //获取前台name="auto_user"参数
        String auto_user = request.getParameter("auto_user");
        //判断用户是否勾选
        if(!"AutoUser".equals(auto_user)){
            //没有勾选
            //创建Cookie
            Cookie cookie = new Cookie("autoUser","") ;
            //有效期:0:表示自动删除cookie
            cookie.setMaxAge(0);
            response.addCookie(cookie);
        }else{
            //定义一个内容:将用户名和密码使用":"拼接
            String content = username+":"+password ;
            //加密:jdk提供的加密工具
           content =  URLEncoder.encode(content,"utf-8") ;
            //勾选了
            Cookie cookie = new Cookie("autoUser",content) ;
            //设置有效期:1个月
            cookie.setMaxAge(60*60*24*30);
            //将cookie信息携带浏览器
            response.addCookie(cookie); //原理:响应头:set-Cookie:content

            //下一次访问:携带Cookie 的请求头: 服务器解析
        }


        //重定向(浏览器)到jsp/index.jsp
        response.sendRedirect(request.getContextPath()+"/");





    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request,response);
    }
}

